package com.explorati.interview.interview.chapterfifteen;

import java.util.LinkedList;
import java.util.List;

public class Circle {

	public static void cycle(int total, int num) {
		List<Integer> list = new LinkedList<Integer>();
		for (int i = 1; i <= total; i++) {
			list.add(i);
		}
		int index = -1;
		while (list.size() > 1) {
			// 得到应该出局的下标
			index = (index + num) % list.size();
			// 将这个元素移除，然后index回到上一个指向
			list.remove(index--);
		}
		System.out.println(list.get(index));
	}

	public static void cycle1(int total, int num) {
		List<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i < total; i++) {
			list.add(i);
		}

		//定义查的数
		int index = 1;
		//若链表的长度大于1
		while (list.size() > 1) {
			//如果此人查的数是num的倍数或者末尾含有此数
			if ((index % num) == 0 || (list.size() % 10) == num) {
				//移除喊这个数的人，因为list角标比index小1
				list.remove((index - 1) % list.size());
			}
			index++;
		}
		System.out.println(list.get(0));
	}

	public static void main(String[] args) {
		cycle(50, 3);
		System.out.println();
		cycle1(10, 3);
	}
}
